package MULE.Model.Logic;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import MULE.Model.*;
import MULE.Model.Panel.*;

/**
 * Logic Module responsible for processing the PubPanel and its inputs.
 * 
 * @author Aaron Englebardt, jstep version 1.0 11.12.2013
 * 
 */

public class PubLogic extends MulePresenter implements ActionListener {

	private static Random rand = new Random();
	private static final int TOP_GAMBLE_TIME = 37;
	private static final int MID_GAMBLE_TIME = 25;
	private static final int LOW_GAMBLE_TIME = 12;
	private static final int LAST_ROUND = 12;
	private static final int LATE_ROUND = 8;
	private static final int EARLY_ROUND = 4;

	public enum ActionCommand {
		ENTER_PUB, GAMBLE, EXIT_PUB
	}

	/**
	 * Creates a new Pub responsible for processing/manipulating the given view.
	 * 
	 * @param view
	 *            The pub panel that this object controls.
	 */
	public PubLogic(PubPanel view) {
		super(view);
		view.addExitButtonListener(this);
		view.addGambleListener(this);
	}

	/**
	 * Computes the semi-random value the current player gets for gambling, adds
	 * the money to their inventory, ends the player's turn, and starts the next
	 * turn.
	 * 
	 * @param p
	 *            The current player.
	 * @param currentTime
	 *            The amount of time left in the current turn as an int in
	 *            seconds.
	 * @param currentRound
	 *            The current round of the game.
	 */
	public void gamble(Player p, int currentTime, int currentRound) {
		// end turn (after the proper time, round, and player are passed in,
		// this can be stopped.)
		MuleDriver.getGameLogic().endTurn();
		// calculate time bonus
		int timeBonus, roundBonus;
		if (currentTime >= TOP_GAMBLE_TIME) {
			timeBonus = 200;
		} else if (currentTime >= MID_GAMBLE_TIME) {
			timeBonus = 150;
		} else if (currentTime >= LOW_GAMBLE_TIME) {
			timeBonus = 100;
		} else {
			timeBonus = 50;
		}
		// calculate round bonus
		if (currentRound == LAST_ROUND) {
			roundBonus = 200;
		} else if (currentRound >= LATE_ROUND) {
			roundBonus = 150;
		} else if (currentRound >= EARLY_ROUND) {
			roundBonus = 100;
		} else {
			roundBonus = 50;
		}
		// calculate money bonus (round bonus * random from 0 to time bonus)
		int moneyBonus = roundBonus * rand.nextInt(timeBonus);
		if (moneyBonus > 250) {// max amount that can be earned by gambling is
								// 250
			moneyBonus = 250;
		}
		// give money to current player and start next turn
		p.getInventory().add(Resource.MONEY, moneyBonus);
		String msg = p.getName() + " gambled remaining turn time and got $"
				+ moneyBonus + ".\n";
		msg += p.getName() + "'s turn is over!";
		MuleDriver.showAlertDialog("Pub", msg);
		MuleDriver.updateScore(p);
		MuleDriver.getGameLogic().nextTurn();
	}

	/**
	 * Event handler. Processes inputs from the land office panel.
	 * 
	 * @param e
	 *            The event which was fired.
	 */
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

		if (command.equals(ActionCommand.ENTER_PUB.toString())) {
			// Pub shows itself.
			MuleDriver.setGameView(view);
		} else if (command.equals(ActionCommand.EXIT_PUB.toString())) {
			// Exit back to town.
			TownPanel townPanel = (TownPanel) MuleDriver.getTownLogic()
					.getView();
			MuleDriver.setGameView(townPanel);
		} else if (command.equals(ActionCommand.GAMBLE.toString())) {
			// Run gamble method
			gamble(MuleDriver.getGameLogic().getCurrentPlayer(), MuleDriver
					.getGameLogic().getRemainingTurnTime(), MuleDriver
					.getGameLogic().getCurrentRound());
			// Exit to map
			MapPanel mapPanel = (MapPanel) MuleDriver.getMapLogic().getView();
			MuleDriver.setGameView(mapPanel);
		}
	}
}
